package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CoReturnListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CoReturnListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-30 13:38
 */
@Service
public class CoReturnListServiceImpl implements CoReturnListService {


    @Autowired
    private CoReturnListGoodsDao coReturnListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;

    //退货单保存
    @Override
    public ServiceVO coReturnListGoodsListSave(CustomerReturnList customerReturnList, List<CustomerReturnListGoods> customerReturnListGoodsList) {

        //保存新增退货单
        coReturnListGoodsDao.saveCoReturnList(customerReturnList);
        Integer customerReturnListId = customerReturnList.getCustomerReturnListId();

        //保存商品信息
        for (CustomerReturnListGoods customerReturnListGoods : customerReturnListGoodsList) {
            //设置进货单
            customerReturnListGoods.setCustomerReturnListId(customerReturnListId);
            //调用保存方法
            coReturnListGoodsDao.saveCoReturnListGoods(customerReturnListGoods);
            //查询状态
            Goods coReGoods = goodsDao.findByGoodsId(customerReturnListGoods.getGoodsId());

            //设置新总库存
            coReGoods.setInventoryQuantity(coReGoods.getInventoryQuantity() + customerReturnListGoods.getGoodsNum());
            coReGoods.setState(2);

            //调用修改方法
            goodsDao.updateGoods(coReGoods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //客户退货单查询
    @Override
    public Map<String, Object> getCoReturnSearchList(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //查询结果返回列表
        List<CustomerReturnList> customerReturnLists = coReturnListGoodsDao.getCoReSearchList(returnNumber, customerId, state, sTime, eTime);
        //将查询结果放入map
        map.put("rows", customerReturnLists);
        //返回map数据
        return map;
    }


    //退货单商品信息
    @Override
    public Map<String, Object> getCoReturnGoodsList(Integer customerReturnListId) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //查询结果返回对象
        List<CustomerReturnListGoods> customerReturnListGoodsList = coReturnListGoodsDao.getCoReGoodsList(customerReturnListId);
        map.put("rows",customerReturnListGoodsList);
        return map;
    }


    //删除退货单
    @Override
    public ServiceVO deleteCoReturnListGoods(Integer customerReturnListId) {
        coReturnListGoodsDao.deleteCoReturnListGoods(customerReturnListId);
        coReturnListGoodsDao.deleteCoReturnList(customerReturnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //修改销售推货单
    @Override
    public ServiceVO updateCoReturnListGoods(Integer customerReturnListId) {
        coReturnListGoodsDao.updateCoReturnList(customerReturnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

}
